<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>我的收藏 - 校园二手交易平台</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 20px;
      background-color: #f5f5f5;
    }
    .container {
      max-width: 800px;
      margin: 0 auto;
      background: white;
      padding: 30px;
      border-radius: 10px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    .header {
      text-align: center;
      margin-bottom: 30px;
    }
    .item-card {
      background: #f9f9f9;
      padding: 15px;
      margin-bottom: 15px;
      border-radius: 5px;
      box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }
    .item-title {
      font-weight: bold;
      font-size: 18px;
    }
    .btn {
      padding: 8px 16px;
      background: #667eea;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }
    .btn:hover {
      background: #5a6fd8;
    }
    .btn-outline {
      padding: 8px 16px;
      background: transparent;
      color: #667eea;
      border: 1px solid #667eea;
      border-radius: 5px;
      cursor: pointer;
    }
    .btn-outline:hover {
      background: #5a6fd8;
      color: white;
    }
  </style>
</head>
<body>
<div class="container">
  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px;">
    <div class="header" style="margin-bottom: 0;">
      <h2>我的收藏</h2>
    </div>
    <a href="index.html" class="btn-outline">返回首页</a>
  </div>
  <div id="favoritesList">
    <!-- 收藏的商品将通过JavaScript动态加载 -->
  </div>
</div>

<script>
  async function loadFavorites() {
    const userData = localStorage.getItem('user');
    if (!userData) {
      window.location.href = 'login.html';
      return;
    }
    const userId = JSON.parse(userData).userId;

    try {
            const token = localStorage.getItem('token');
      const headers = {};
      if (token) {
          headers['Authorization'] = 'Bearer ' + token;
      }
      const response = await fetch(`/api/favorites/user/${userId}`, { headers });
      const favorites = await response.json();
      const favoritesList = document.getElementById('favoritesList');
      favoritesList.innerHTML = '';

      favorites.forEach(favorite => {
        const itemCard = document.createElement('div');
        itemCard.className = 'item-card';

        // Add image display
        let imageHtml = '';
        if (favorite.item && favorite.item.base64Image) {
            imageHtml = `<img src="${favorite.item.base64Image}" alt="商品图片" style="max-width: 100px; max-height: 100px; margin-right: 15px; border-radius: 5px; object-fit: cover;"/>`;
        } else {
             imageHtml = `<div style="width: 100px; height: 100px; background-color: #eee; display: flex; align-items: center; justify-content: center; margin-right: 15px; border-radius: 5px; color: #888; font-size: 12px;">暂无图片</div>`;
        }

        itemCard.innerHTML = `
                    <div style="display: flex; align-items: center;">
                        ${imageHtml}
                        <div style="flex-grow: 1;">
                            <div class="item-title">${favorite.item ? favorite.item.title : '加载失败'}</div>
                            <div>价格: ¥${favorite.item ? favorite.item.price : 'N/A'}</div>
                        </div>
                        <button class="btn" onclick="removeFavorite(${favorite.favoriteId})">取消收藏</button>
                    </div>
                `;
        favoritesList.appendChild(itemCard);
      });
    } catch (error) {
      console.error('加载收藏失败:', error);
    }
  }

  async function removeFavorite(favoriteId) {
    try {
      const userData = localStorage.getItem('user');
      const userId = JSON.parse(userData).userId;
            const token = localStorage.getItem('token');
      const headers = {};
      if (token) {
          headers['Authorization'] = 'Bearer ' + token;
      }
      await fetch(`/api/favorites/user/${userId}/item/${favoriteId}`, {
        method: 'DELETE',
        headers: headers
      });
      loadFavorites(); // 重新加载收藏列表
    } catch (error) {
      console.error('取消收藏失败:', error);
    }
  }

  window.addEventListener('load', loadFavorites);
</script>
</body>
</html>